/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.jSkyRoom.generator;

import java.util.ArrayList;
import java.util.Collection;
import ru.jSkyRoom.generator.command.rule.Rule;
import ru.jSkyRoom.interfaces.major.Major;
import ru.jSkyRoom.interfaces.support.Unmodifiable;

/**
 *
 * @author Rakev
 */
public class SimpleItemsGenerator<T extends Major> implements ItemsGenerator<T> {

    private Collection<Rule<T>> generationRules;

    public SimpleItemsGenerator(Collection<Rule<T>> generationRules) {
        this.generationRules = generationRules;
    }
    
    @Override
    public Collection<T> generate(Collection<T> baseItems) {
         Collection<T> generatedItems = new ArrayList<>(generationRules.size()*baseItems.size());
        for(T baseItem : baseItems){
            Collection<T> newGeneratedItems = generate(baseItem);
            generatedItems.addAll(newGeneratedItems);
        }
        return generatedItems;
    }

    private  Collection<T> generate(T baseItem) {
         Collection<T> generatedItems = new ArrayList<>(generationRules.size());
        for (Rule<T> rule : generationRules){
            if (rule.check(baseItem)){
                T generatedItem = rule.apply(baseItem);
                generatedItems.add(generatedItem);
            }
        }
        return generatedItems;
    }
    
}
